package project.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import schronisko.Queries;

/**
 * Servlet implementation class Admin_Functions
 */
@WebServlet("/Admin_Functions")
public class Admin_Functions extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	Queries q = new Queries();
	HttpSession s;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Admin_Functions() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		s = request.getSession();
		String type = request.getParameter("type");
		switch(type){
		case "clean":
			int confirm = Integer.parseInt(request.getParameter("confirm"));
			if(confirm == 1) {
				int success = q.cleanBase();
				if(success == 1)
					s.setAttribute("cleaned", 1);
				else
					s.setAttribute("cleaned", 2);
			}
			else if(confirm == 0)
				s.setAttribute("cleaned", 2);
			response.sendRedirect("admin/clean.jsp");
			break;
		case "delete_anim":
			int ID_animal = Integer.parseInt(request.getParameter("anim"));
			if(ID_animal > 0){
				System.out.println(ID_animal);
				int rows = q.deleteAnimal(ID_animal);
				if(rows == 1) {
					s.setAttribute("deleted_anim", 1);
				}
				else
					s.setAttribute("deleted_anim", 2);
				response.sendRedirect("admin/delete_animal.jsp");
				break;
			}
		case "delete_user":
			String username = request.getParameter("login");
			int ID_user = q.GetUserID(username);
			if(ID_user != -1){
				int success = q.deleteUser(ID_user);
				if(success == 1) {
					s.setAttribute("deleted_user", 1);
				}
				else {
					s.setAttribute("deleted_user", 2);
				}
			}
			else 
				s.setAttribute("deleted_user", 3);
			response.sendRedirect("admin/delete_client.jsp");
			break;
		}
	}

}
